package Listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.tree.MutableTreeNode;

import View.ServerScreen;
import View.TableTab;

public class TreeListener implements ActionListener, MouseListener{
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		//int selRow = ServerScreen.tree.getRowForLocation(e.getX(), e.getY());
		ServerScreen.selPath = ServerScreen.tree.getPathForLocation(e.getX(), e.getY());	
		System.out.println(ServerScreen.selPath);
		
		if(SwingUtilities.isRightMouseButton(e)){
			ServerScreen.selPath = ServerScreen.tree.getPathForLocation(e.getX(), e.getY());
		}
		
		//double click open tab
		if(e.getClickCount() == 2){
			if(ServerScreen.selPath != null){
				MutableTreeNode node = (MutableTreeNode) ServerScreen.selPath
						.getLastPathComponent();
				if(node != ServerScreen.top){
					for(int i = 0; i < ServerScreen.top.getChildCount(); i++){
						if(node == ServerScreen.top.getChildAt(i)){
							//chua co table nao duoc mo
							if(ServerScreen.tableOpened.size() == 0){
								//ServerScreen.createTab(node.toString());	
								TableTab newTab = new TableTab(node.toString());
								ServerScreen.tableOpened.add(node.toString());
								break;
							}else{
								int count = 0;
								for(int j = 0; j < ServerScreen.tableOpened.size(); j++){
									String item =  (String)ServerScreen.tableOpened.elementAt(j);
									if(item.compareTo(node.toString())!=0){
										count++;
									}
								}
								//table duoc click vao chua mo
								if(count == ServerScreen.tableOpened.size()){
									//ServerScreen.createTab(node.toString());
									TableTab newTab = new TableTab(node.toString());
									ServerScreen.tableOpened.add(node.toString());
									break;
								}
							}					
						}
					}
				}
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
